#!/usr/bin/env python3
"""
创建macOS .icns图标文件
"""
from PIL import Image
import os
import subprocess

def create_icns():
    """创建.icns图标文件"""
    # 检查是否有sips命令（macOS系统工具）
    try:
        subprocess.run(['which', 'sips'], check=True, capture_output=True)
        has_sips = True
    except:
        has_sips = False
    
    if not has_sips:
        print("❌ 需要macOS系统来创建.icns文件")
        return False
    
    # 创建iconset目录
    iconset_dir = 'assets/icon.iconset'
    os.makedirs(iconset_dir, exist_ok=True)
    
    # 需要的图标尺寸
    sizes = [
        (16, 'icon_16x16.png'),
        (32, 'icon_16x16@2x.png'),
        (32, 'icon_32x32.png'),
        (64, 'icon_32x32@2x.png'),
        (128, 'icon_128x128.png'),
        (256, 'icon_128x128@2x.png'),
        (256, 'icon_256x256.png'),
        (512, 'icon_256x256@2x.png'),
        (512, 'icon_512x512.png'),
        (1024, 'icon_512x512@2x.png')
    ]
    
    # 加载原始图标
    try:
        img = Image.open('assets/icon.png')
        print(f"✅ 原始图标尺寸: {img.size}")
    except Exception as e:
        print(f"❌ 无法加载原始图标: {e}")
        return False
    
    # 创建各种尺寸的图标
    for size, filename in sizes:
        try:
            resized = img.resize((size, size), Image.Resampling.LANCZOS)
            output_path = os.path.join(iconset_dir, filename)
            resized.save(output_path)
            print(f"✅ 创建 {filename} ({size}x{size})")
        except Exception as e:
            print(f"❌ 创建 {filename} 失败: {e}")
    
    # 使用sips创建.icns文件
    try:
        cmd = ['sips', '-s', 'format', 'icns', iconset_dir, '--out', 'assets/icon.icns']
        result = subprocess.run(cmd, check=True, capture_output=True, text=True)
        print("✅ 成功创建 icon.icns")
        
        # 清理临时目录
        import shutil
        shutil.rmtree(iconset_dir)
        print("✅ 清理临时文件")
        
        return True
    except subprocess.CalledProcessError as e:
        print(f"❌ 创建.icns文件失败: {e}")
        return False

if __name__ == "__main__":
    print("🎨 创建macOS图标文件...")
    if create_icns():
        print("🎉 图标创建完成！")
    else:
        print("❌ 图标创建失败")
